package com.jnevision.extra.controller;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.StringJoiner;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jnevision.common.CommonQuery;
import com.jnevision.extra.model.Extra;
import com.jnevision.extra.service.ExtraServiceImpl;
import com.jnevision.user.model.User;
import com.jnevision.user.service.UserServiceImpl;
import com.jnevision.userdept.model.UserDept;
import com.jnevision.userdept.service.UserDeptServiceImpl;
import com.jnevision.util.Excels;

/**
 * <p>
 * 它得收入 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-14
 */

@RestController
@RequestMapping("/extras")
public class ExtraController extends ApiController {
    @Autowired
    private ExtraServiceImpl extraServiceImpl;
    @Autowired
    private UserServiceImpl userServiceImpl;
    @Autowired
    private UserDeptServiceImpl userDeptServiceImpl;

    @PostMapping
    public R<Boolean> add(@RequestBody Extra extra) {
    	User user = this.userServiceImpl.getOne(new QueryWrapper<User>().eq("phone", extra.getPhone()));
        if (user != null) {
            extra.setUserId(user.getId());
            UserDept ud =
                this.userDeptServiceImpl.getOne(new QueryWrapper<UserDept>().eq("user_id", user.getId()));
            extra.setDeptId(ud.getDeptId());
            return this.success(this.extraServiceImpl.save(extra));
        }
        return R.failed("手机号码不存在");
    }

    @PutMapping
    public R<Boolean> update(@RequestBody Extra extra) {
        return this.success(this.extraServiceImpl.updateById(extra));
    }

    @DeleteMapping
    public R<Boolean> delete(String id) {
        return this.success(this.extraServiceImpl.removeById(id));
    }

    @GetMapping
    public R<Extra> get(String id) {
    	
    	Extra byId = this.extraServiceImpl.getById(id);
    	
    	User byId2 = this.userServiceImpl.getById(byId.getUserId());
    	if (null!=byId2&&StringUtils.isNotEmpty(byId2.getName())) {
    		byId.setUserName(byId2.getName());
    		byId.setPhone(byId2.getPhone());
    	}
    	
        return this.success(byId);
    }

    /**
     * 它得 的检索
     * 
     * @param page
     * @param extra
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Extra>> page(Page<Extra> page, CommonQuery query) {
        return this.success(this.extraServiceImpl.page(page, query));
    }

    /**
     * 它得明细导入
     * 
     * @param file
     * @return
     */
    @PostMapping("/uploadfile")
    public R<Boolean> uploadfile(@RequestParam("files") MultipartFile files) {
        LinkedHashMap<String, String> title = new LinkedHashMap<>();
        title.put("姓名", "userName");
        title.put("部门", "deptName");
        title.put("电话", "phone");
        title.put("类别", "typeName");
        title.put("金额(元)", "money");
        title.put("日期", "extraTime");
        title.put("备注", "remark");
        try {
            List<Extra> list = Excels.read(files.getInputStream(), title, Extra.class);

            StringJoiner sj = new StringJoiner(",");
            for (Extra extra : list) {
                User user = this.userServiceImpl.getOne(new QueryWrapper<User>().eq("phone", extra.getPhone()));
                if (user != null) {
                    extra.setUserId(user.getId());
                    UserDept ud =
                        this.userDeptServiceImpl.getOne(new QueryWrapper<UserDept>().eq("user_id", user.getId()));
                    extra.setDeptId(ud.getDeptId());
                } else {
                    sj.add(extra.getPhone());
                }
            }

            if (StringUtils.isBlank(sj.toString())) {
                return this.success(this.extraServiceImpl.saveBatch(list));
            } else {
                sj.add("这些手机号没有对应的账号，请核对");
                return this.failed(sj.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this.failed("批量导入它得明细异常");
    }
    
    @GetMapping("/{state}")
    public R<Integer> countExtra(@PathVariable Integer state){
    	
    	return this.success(this.extraServiceImpl.count(new QueryWrapper<Extra>().eq("state", state)));
    }

}
